package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.CurrentUserConstants;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.RoutePlanningService;
import com.yuwubao.uav.tool.service.TaskService;
import com.yuwubao.uav.tool.service.UserService;
import com.yuwubao.uav.tool.util.MD5Util;
import com.yuwubao.uav.tool.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.catalina.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.security.provider.MD5;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;

/**
 *
 */
@RestController
@RequestMapping("/bk")
public class UserController {

    private static final Logger logger = LogManager.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @GetMapping("/listRoleForAddUser")
    @ApiOperation(value = "为添加用户和注册用户获取角色列表")
    public JsonResult listFrontUserRole(
            @ApiParam(value = "角色类型 0：后端用户；1：前端用户", required = true) @RequestParam(value = "flag") String flag
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pd = new PageData();
            pd.put("flag",flag);
            result.setResult(userService.listFrontUserRole(pd));
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @PostMapping("/checkUserNameExist")
    @ApiOperation(value = "检查用户名是否可用")
    public JsonResult checkUserNameExist(
            @ApiParam(value = "用户名", required = true) @RequestParam(value = "userName") String userName
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pd = new PageData();
            pd.put("userName",userName);

            //校验用户名是否存在
            int count = userService.getUserByUserName(pd);
            if (count > 0) {
                result.setFailure("该用户名已经存在");
                return result;
            }
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @PostMapping("/addFrontUser")
    @ApiOperation(value = "用户注册")
    public JsonResult addFrontUser(
            @ApiParam(value = "用户名", required = true) @RequestParam(value = "userName") String userName,
            @ApiParam(value = "密码", required = true) @RequestParam(value = "userPwd") String userPwd,
            @ApiParam(value = "用户角色ID") @RequestParam(value = "userRoleId") String userRoleId,
            @ApiParam(value = "机构名称") @RequestParam(value = "organizationName",required = false) String organizationName,
            @ApiParam(value = "联系人") @RequestParam(value = "contactName",required = false) String contactName,
            @ApiParam(value = "邮箱") @RequestParam(value = "email",required = false) String email,
            @ApiParam(value = "所属机构ID") @RequestParam(value = "parentUserId",required = false) String parentUserId,
            @ApiParam(value = "真实姓名") @RequestParam(value = "realName",required = false) String realName,
            @ApiParam(value = "公司姓名") @RequestParam(value = "companyName",required = false) String companyName,
            @ApiParam(value = "用户头像链接") @RequestParam(value = "userImg",required = false) String userImg,
            @ApiParam(value = "用户研究领域ID，多个用逗号分隔") @RequestParam(value = "researchFieldIds",required = false) String researchFieldIds,
            @ApiParam(value = "联系电话") @RequestParam(value = "userTel",required = false) String userTel
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pd = new PageData();
            pd.put("userName",userName);
            pd.put("userPwdMW",userPwd);
            pd.put("userPwd",MD5Util.md5(userPwd));
            pd.put("userTel",userTel);
            pd.put("organizationName",organizationName);
            pd.put("contactName",contactName);
            pd.put("email",email);
            pd.put("parentUserId",parentUserId);
            pd.put("realName",realName);
            pd.put("companyName",companyName);
            pd.put("userImg",userImg);
            pd.put("userTel",userTel);
            pd.put("roleId",userRoleId);
            pd.put("researchFieldIds",researchFieldIds);

            if ("1".equals(userRoleId) || "2".equals(userRoleId) || "3".equals(userRoleId) || "4".equals(userRoleId)) {
                pd.put("flag","0");
            } else {
                pd.put("flag","1");
            }

            //校验用户名是否存在
            int count = userService.getUserByUserName(pd);
            if (count > 0) {
                result.setFailure("该用户名已经存在");
                return result;
            }

            //校验邮箱是否重复
            int count1 = userService.getUserByMail(pd);
            if (count1 > 0) {
                result.setFailure("该邮箱已经注册");
                return result;
            }


            userService.addUser(pd);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/changePwd")
    @ApiOperation(value = "前端用户修改密码")
    public JsonResult changePwd(
            @ApiParam(value = "旧密码", required = true) @RequestParam(value = "userName") String userPwdOld,
            @ApiParam(value = "新密码", required = true) @RequestParam(value = "userPwd") String userPwdNew,
            @ApiParam(value = "用户ID",required = true) @RequestParam(value = "userId") String userId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pd = new PageData();
            pd.put("userPwdOld",MD5Util.md5(userPwdOld));
            pd.put("userPwdNew",MD5Util.md5(userPwdNew));
            pd.put("userPwdMw",userPwdNew);
            pd.put("userId",userId);

            if (userPwdOld.equals(userService.getUserPwdById(pd))) {
                userService.changePwd(pd);
                result.setSuccessful();
            } else {
                result.setFailure("原密码错误");
            }

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listFrontUser")
    @ApiOperation(value = "获取前端用户列表")
    public JsonResult listFrontUser(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 2) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(userService.listFrontUser(pageData));
            result.setResultNum(userService.listFrontUserCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/deleteUser")
    @ApiOperation(value = "删除用户")
    public JsonResult deleteUser(
            @ApiParam(value = "用户ID",required = true) @RequestParam(value = "userId") String userId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("userId", userId);

            userService.deleteUser(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableUser")
    @ApiOperation(value = "禁用或者激活禁用的用户")
    public JsonResult disableOrEnableUser(
            @ApiParam(value = "用户ID",required = true) @RequestParam(value = "userId") String userId,
            @ApiParam(value = "0:禁用 1：激活",required = true) @RequestParam(value = "state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("userId", userId);
            pageData.put("state", state);

            userService.disableOrEnableUser(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public JsonResult login(
            @ApiParam(value = "用户姓名",required = true) @RequestParam(value = "userName") String userName,
            @ApiParam(value = "用户密码",required = true) @RequestParam(value = "userPwd") String userPwd,
            @ApiParam(value = "0:后端 1：前端",required = true) @RequestParam(value = "loginType") String loginType
    ) {
        JsonResult result = new JsonResult();

        try {
            PageData pageData = new PageData();
            pageData.put("userName", userName);
            pageData.put("userPwd", MD5Util.md5(userPwd));
            pageData.put("loginType", loginType);
            PageData pd =userService.login(pageData);
            if (pd != null && pd.get("token") != null) {
                result.setResult(pd);
                result.setSuccessful();
            } else {
                result.setFailure("用户名或者密码错误");
            }

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listBackendUser")
    @ApiOperation(value = "获取后端用户列表")
    public JsonResult listBackendUser(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 2) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(userService.listBackendUser(pageData));
            result.setResultNum(userService.listBackendUserCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/updateBackendUser")
    @ApiOperation(value = "编辑后端用户信息")
    public JsonResult updateBackendUser(
            @ApiParam(value = "用户ID", required = true) @RequestParam("userId") String userId,
            @ApiParam(value = "用户密码") @RequestParam(value = "userPwd",required = false) String userPwd,
            @ApiParam(value = "角色Id") @RequestParam(value = "roleId",required = false) String roleId,
            @ApiParam(value = "所属平台或机构ID") @RequestParam(value = "organizationId",required = false) String organizationId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("userId", userId);
            pageData.put("userPwdMw", userPwd);
            pageData.put("userPwd", MD5Util.md5(userPwd));
            pageData.put("roleId", roleId);
            pageData.put("organizationId", organizationId);

            userService.updateBackendUser(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


    @LoginRequired
    @PostMapping("/addPrivilege")
    @ApiOperation(value = "新增权限")
    public JsonResult addPrivilege(
            @ApiParam(value = "权限名称", required = true) @RequestParam("privilegeName") String privilegeName
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("privilegeName", privilegeName);

            userService.addPrivilege(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updatePrivilege")
    @ApiOperation(value = "更新权限")
    public JsonResult updatePrivilege(
            @ApiParam(value = "权限ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "权限名称", required = true) @RequestParam("privilegeName") String privilegeName
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("privilegeName", privilegeName);

            userService.updatePrivilege(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deletePrivilege")
    @ApiOperation(value = "删除权限")
    public JsonResult deletePrivilege(
            @ApiParam(value = "权限ID", required = true) @RequestParam("id") String id
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);

            userService.deletePrivilege(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listPrivilege")
    @ApiOperation(value = "获取权限列表")
    public JsonResult listPrivilege(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 2) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(userService.listPrivilege(pageData));
            result.setResultNum(userService.listPrivilegeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


    @LoginRequired
    @PostMapping("/addRole")
    @ApiOperation(value = "新增角色")
    public JsonResult addRole(
            @ApiParam(value = "角色名称", required = true) @RequestParam("roleName") String roleName,
            @ApiParam(value = "角色类型 0：后台角色 1：前台角色", required = true) @RequestParam("flag") String flag
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("roleName", roleName);
            pageData.put("flag", flag);

            userService.addRole(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateRole")
    @ApiOperation(value = "更新角色")
    public JsonResult updateRole(
            @ApiParam(value = "角色ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "角色名称", required = true) @RequestParam("roleName") String roleName,
            @ApiParam(value = "角色类型 0：后台角色 1：前台角色", required = true) @RequestParam("flag") String flag
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("roleName", roleName);
            pageData.put("flag", flag);

            userService.updateRole(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteRole")
    @ApiOperation(value = "删除角色")
    public JsonResult deleteRole(
            @ApiParam(value = "角色ID", required = true) @RequestParam("id") String id
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);

            if (!userService.checkRoleIsUsing(pageData)) {
                result.setFailure("角色有用户使用，不能删除");
                return result;
            }
            userService.deleteRole(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listRole")
    @ApiOperation(value = "获取角色列表")
    public JsonResult listRole(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 2) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(userService.listRole(pageData));
            result.setResultNum(userService.listRoleCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


    @LoginRequired
    @PostMapping("/addPrivilegeToRole")
    @ApiOperation(value = "更新角色添加权限")
    public JsonResult addPrivilegeToRole(
            @ApiParam(value = "角色ID", required = true) @RequestParam("roleId") String roleId,
            @ApiParam(value = "权限ID，多个用逗号隔开", required = true) @RequestParam("privilegeId") String privilegeId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("roleId", roleId);
            pageData.put("privilegeId", privilegeId);

            userService.addPrivilegeToRole(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrganizationForUser")
    @ApiOperation(value = "获取机构列表")
    public JsonResult listOrganizationForUser(
            @ApiParam(value = "机构角色ID", required = true) @RequestParam("roleId") String roleId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("roleId", roleId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(userService.listOrganization(pageData));
            result.setResultNum(userService.listOrganizationCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listUserPrivilege")
    @ApiOperation(value = "获取用户权限列表")
    public JsonResult listUserPrivilege(
            @ApiParam(value = "机构角色ID", required = true) @RequestParam("userId") String userId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("userId", userId);

            result.setResult(userService.listUserPrivilege(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listUserBrowsingTypeHistory")
    @ApiOperation(value = "获取用户浏览历史记录类型列表")
    public JsonResult listUserBrowsingTypeHistory(
            HttpServletRequest request
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("userId",userIdObj.toString());

                result.setResult(userService.listUserBrowsingTypeHistory(pageData));

                result.setSuccessful();
            } else {
              result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listUserBrowsingHistory")
    @ApiOperation(value = "获取用户浏览历史记录列表")
    public JsonResult listUserBrowsingHistory(
            HttpServletRequest request,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey,
            @ApiParam(value = "浏览记录类型") @RequestParam(value = "typeName",required = false) String typeName
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("searchKey", searchKey);
                pageData.put("typeName", typeName);
                pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
                pageData.put("pageSize", Integer.valueOf(pageSize));
                pageData.put("userId",userIdObj.toString());

                result.setResult(userService.listUserBrowsingHistory(pageData));
                result.setResultNum(userService.listUserBrowsingHistoryCount(pageData));

                result.setSuccessful();
            } else {
              result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addUserBrowsingHistoryType")
    @ApiOperation(value = "新增用户浏览历史记录类型名称")
    public JsonResult addUserBrowsingHistoryType(
            HttpServletRequest request,
            @ApiParam(value = "浏览类型名称", required = true) @NotEmpty @RequestParam("typeName") String typeName
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("typeName", typeName);
                pageData.put("userId",userIdObj.toString());

                userService.addUserBrowsingHistoryType(pageData);

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/updateUserBrowsingHistoryTypeName")
    @ApiOperation(value = "修改用户浏览历史记录类型名称")
    public JsonResult updateUserBrowsingHistoryTypeName(
            HttpServletRequest request,
            @ApiParam(value = "旧类型名称", required = true) @NotEmpty @RequestParam("oldTypeName") String oldTypeName,
            @ApiParam(value = "新类型名称", required = true) @NotEmpty @RequestParam("newTypeName") String newTypeName
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("oldTypeName", oldTypeName);
                pageData.put("newTypeName", newTypeName);
                pageData.put("userId",userIdObj.toString());

                userService.updateUserBrowsingHistoryTypeName(pageData);

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/updateUserBrowsingHistoryType")
    @ApiOperation(value = "修改用户浏览历史记录所属类型")
    public JsonResult updateUserBrowsingHistoryType(
            HttpServletRequest request,
            @ApiParam(value = "浏览记录ID", required = true) @NotEmpty @RequestParam("id") String id,
            @ApiParam(value = "新类型名称", required = true) @NotEmpty @RequestParam("newTypeName") String newTypeName
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("id", id);
                pageData.put("newTypeName", newTypeName);
                pageData.put("userId",userIdObj.toString());

                userService.updateUserBrowsingHistoryType(pageData);

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/deleteUserBrowsingHistoryType")
    @ApiOperation(value = "删除用户浏览历史记录类型名称")
    public JsonResult deleteUserBrowsingHistoryType(
            HttpServletRequest request,
            @ApiParam(value = "类型名称", required = true) @NotEmpty @RequestParam("typeName") String typeName
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("oldTypeName", typeName);
                pageData.put("newTypeName", "未分类");
                pageData.put("userId",userIdObj.toString());

                userService.deleteUserBrowsingHistoryType(pageData);

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }



    @LoginRequired
    @GetMapping("/getUserBrowsingHistory")
    @ApiOperation(value = "获取用户浏览历史记录详情")
    public JsonResult getUserBrowsingHistory(
            @ApiParam(value = "表名", required = true) @NotEmpty @RequestParam("tableName") String tableName,
            @ApiParam(value = "record_id", required = true) @NotEmpty @RequestParam("id") String id
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("tableName", tableName);
            pageData.put("id", id);

            result.setResult(userService.getUserBrowsingHistory(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/deleteUserBrowsingHistory")
    @ApiOperation(value = "删除用户浏览历史记录")
    public JsonResult deleteUserBrowsingHistory(
            @ApiParam(value = "id", required = true) @NotEmpty @RequestParam("id") String id
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);

            userService.deleteUserBrowsingHistory(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addUserCollection")
    @ApiOperation(value = "添加用户收藏记录")
    public JsonResult addUserCollection(
            HttpServletRequest request,
            @ApiParam(value = "表名", required = true) @NotEmpty @RequestParam("tableName") String tableName,
            @ApiParam(value = "record_id", required = true) @NotEmpty @RequestParam("recordId") String recordId,
            @ApiParam(value = "title", required = true) @NotEmpty @RequestParam("title") String title
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("tableName", tableName);
                pageData.put("id", recordId);
                pageData.put("title", title);
                pageData.put("userId",userIdObj.toString());

                userService.addUserCollection(pageData);

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listUserCollection")
    @ApiOperation(value = "获取用户收藏记录列表")
    public JsonResult listUserCollection(
            HttpServletRequest request,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("searchKey", searchKey);
                pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
                pageData.put("pageSize", Integer.valueOf(pageSize));
                pageData.put("userId",userIdObj.toString());

                result.setResult(userService.listUserCollection(pageData));
                result.setResultNum(userService.listUserCollectionCount(pageData));

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listUserIntegration")
    @ApiOperation(value = "获取用户积分记录列表")
    public JsonResult listUserIntegration(
            HttpServletRequest request,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("searchKey", searchKey);
                pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
                pageData.put("pageSize", Integer.valueOf(pageSize));
                pageData.put("userId",userIdObj.toString());

                result.setResult(userService.listUserIntegration(pageData));
                result.setResultNum(userService.listUserIntegrationCount(pageData));

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/getUserIntegrationCount")
    @ApiOperation(value = "获取用户积分记录总数")
    public JsonResult getUserIntegrationCount(
            HttpServletRequest request
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("userId",userIdObj.toString());

                result.setResult(userService.getUserIntegrationCount(pageData));

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listUserPublish")
    @ApiOperation(value = "获取用户发布记录列表")
    public JsonResult listUserPublish(
            HttpServletRequest request,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("searchKey", searchKey);
                pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
                pageData.put("pageSize", Integer.valueOf(pageSize));
                pageData.put("userId",userIdObj.toString());

                result.setResult(userService.listUserPublish(pageData));
                result.setResultNum(userService.listUserPublishCount(pageData));

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/userAdviceEnroll")
    @ApiOperation(value = "用户通知报名")
    public JsonResult userAdviceEnroll(
            HttpServletRequest request,
            @ApiParam(value = "通知ID", required = true) @NotEmpty @RequestParam("adviceId") String adviceId,
            @ApiParam(value = "姓名", required = true) @NotEmpty @RequestParam("userName") String userName,
            @ApiParam(value = "手机号码", required = true) @NotEmpty @RequestParam(value = "userTel") String userTel
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("adviceId", adviceId);
                pageData.put("userName", userName);
                pageData.put("userTel", userTel);
                pageData.put("userId",userIdObj.toString());

                int count = userService.getAdviceEnrollCount(pageData);
                if (count == 0) {
                    userService.userAdviceEnroll(pageData);
                    result.setSuccessful();
                } else {
                    result.setFailure("您已经报过名了，不要重复报名");
                }

            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


    @LoginRequired
    @PostMapping("/addUserPublish")
    @ApiOperation(value = "新增用户发布")
    public JsonResult addUserPublish(
            HttpServletRequest request,
            @ApiParam(value = "说明", required = true) @NotEmpty @RequestParam(value = "title") String title,
            @ApiParam(value = "类型1:图片 2：视频", required = true) @NotEmpty @RequestParam(value = "type") String type,
            @ApiParam(value = "图片或者视频url", required = true) @NotEmpty @RequestParam("url") String url
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("title", title);
                pageData.put("type", type);
                pageData.put("url", url);
                pageData.put("userId",userIdObj.toString());

                userService.addUserPublish(pageData);

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listUserPublishForAdmin")
    @ApiOperation(value = "获取用户发布列表管理员用")
    public JsonResult listUserPublishForAdmin(
            HttpServletRequest request,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("searchKey", searchKey);
                pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
                pageData.put("pageSize", Integer.valueOf(pageSize));
                pageData.put("userId",userIdObj.toString());

                result.setResult(userService.listUserPublishForAdmin(pageData));
                result.setResultNum(userService.listUserPublishForAdminCount(pageData));

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


    @LoginRequired
    @GetMapping("/checkUserPublish")
    @ApiOperation(value = "审核用户发布")
    public JsonResult checkUserPublish(
            HttpServletRequest request,
            @ApiParam(value = "审核结果 0：不通过，1：通过", required = true) @NotEmpty @RequestParam("checkResult") String checkResult,
            @ApiParam(value = "发布的用户的ID", required = true) @NotEmpty @RequestParam("userId") String userId,
            @ApiParam(value = "id", required = true) @NotEmpty @RequestParam("id") String id
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("checkResult", checkResult);
                pageData.put("id", id);
                pageData.put("userId",userId);

                userService.checkUserPublish(pageData,checkResult);

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listMyPublish")
    @ApiOperation(value = "获取我的发布列表")
    public JsonResult listMyPublish(
            HttpServletRequest request,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("searchKey", searchKey);
                pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
                pageData.put("pageSize", Integer.valueOf(pageSize));
                pageData.put("userId",userIdObj.toString());

                result.setResult(userService.listMyPublish(pageData));
                result.setResultNum(userService.listMyPublishCount(pageData));

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addZhuanfaIntegration")
    @ApiOperation(value = "用户转发添加积分")
    public JsonResult addZhuanfaIntegration(
            HttpServletRequest request
    ) {
        JsonResult result = new JsonResult();
        try {

            Object userIdObj = request.getAttribute(CurrentUserConstants.CURRENT_USER);

            if (userIdObj != null) {

                PageData pageData = new PageData();
                pageData.put("userId",userIdObj.toString());

                userService.addZhuanfaIntegration(pageData);

                result.setSuccessful();
            } else {
                result.setFailure("获取用户登录信息失败");
            }


        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }





}
